///<reference path="../../app/app.general.ts"/>
import {Component} from '@angular/core';
import {AlertController, IonicPage, NavController, NavParams} from 'ionic-angular';
import {LoginService} from "./login.service";
import {Storage} from "@ionic/storage";
import {AppUrl, STORAGE_KEY, TIME_OUT} from "../../app/app.general";
import {AppConfig} from "../../app/appConfig";
import {HttpClient} from "@angular/common/http";
import {ACLService} from "../../app/acl/acl.service";
import 'rxjs/add/operator/timeout'

/**
 * Generated class for the LoginPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-login',
  templateUrl: 'login.html',
})
export class LoginPage {

  private user = {
    username: '',
    password: ''
  };

  confirmPassword;
  constructor(public navCtrl: NavController, public navParams: NavParams, private loginService: LoginService, private storage: Storage, private appConfig: AppConfig, private http: HttpClient, private acl: ACLService, private alertCtrl:AlertController) {
    if ('WebSocket' in window) {
      console.log('支持websocket......');
    }
    else {
      alert('不支持');
    }

  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad LoginPage');
  }
  submitType = '登录';

  isConfirm: Boolean;

  setPassword(){
     this.isConfirm = true;
     this.user.password = '';
     this.submitType = '重置';
  }

  showAlert(message) {
    let alert = this.alertCtrl.create({
      title: '警告',
      subTitle: message,
      buttons: ['确定']
    });
    alert.present();
  }


  logIn() {
    if(this.submitType == '登录'){
      this.appConfig.presentLoading('正在登录', TIME_OUT);
      this.loginService.login(this.user).subscribe(res => {
        if (res.code !== 200) {
          this.appConfig.presentToast(res.msg);
          this.appConfig.loader.dismiss();
        }
        else {
          localStorage.setItem(STORAGE_KEY.token, res.data.token);
          localStorage.setItem('userid', res.data.userInfo.userid);
          if (res.data.restaurantInfo)
            localStorage.setItem('restaurantid', res.data.restaurantInfo[0].id);
          // this.appConfig.presentToast('登录成功');
          this.acl.setRoles([res.data.userInfo.roletype]);
          this.acl.setAbilities(res.data.abilities);
          this.storage.set(STORAGE_KEY.userName, res.data.userInfo.username);
          this.navCtrl.setRoot('TabsPage', {}, {animate: true, direction: 'forward'}).then(() => {
            this.appConfig.loader.dismiss();
          });
        }
      }, reject => {
        console.log(reject);
        if (reject.name && reject.name == 'TimeoutError')
          this.appConfig.presentToast('登录超时,请检查网络设置');
        else
          this.appConfig.presentToast('用户名或密码错误,请重试');
        this.appConfig.loader.dismiss();
      });
    }

    if(this.submitType == '重置'){

      if(this.user.username == null || this.user.password == null || this.confirmPassword ==null){
         this.showAlert("带*为必填项");
         return;
      }
      if(this.user.username.trim() == ''){
        this.showAlert("请输入正确的用户名");
        return;
      }
      if(this.user.password.trim() == ''){
        this.showAlert("请输入密码");
        return;
      }
      if(this.confirmPassword.trim() == ''){
        this.showAlert("请输入确认密码");
        return;
      }
      if(this.confirmPassword != this.user.password){
        this.showAlert("确认密码与密码不一致");
        return;
      }
      this.isConfirm = false;
      this.submitType = '登录';
    }
  }
}
